import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class OrderBinarryTree {
    class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public int i;
    public TreeNode createTree(String str){
        TreeNode root = null;

        if (str.charAt(i) != '#') {
            /*
             * 节点才创建 所以不需要判断所有节点是否为空不是判断左右节点是否有值
             * */
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }

    //层序遍历非递归
    public void levelOrder(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }

        System.out.println();
    }

    //层序遍历递归
    public void levelOrder1(TreeNode root){
        if(root == null){
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        if (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            levelOrder1(root.left);
            levelOrder1(root.right);
        }else {
            return;
        }
    }

    //非递归
    List<List<Character>> list = new ArrayList<>();
    public List<List<Character>> levelOrder2(TreeNode root) {
        if(root == null){
            return list;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            List<Character> tmp = new ArrayList<>();
            int size = queue.size();
            while (size != 0){
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            list.add(tmp);
        }

        return list;
    }

    //非递归
    List<List<Character>> list1 = new ArrayList<>();
    public List<List<Character>> levelOrder3(TreeNode root) {
        if(root == null){
            return list;
        }

        OrderTreeNode(root,0);
        return list;
    }

    private void OrderTreeNode(TreeNode root,int depth){
        if(root == null){
            return;
        }

        if(list.size() == depth){
            list.add(new ArrayList<>());
        }

        list.get(depth).add(root.val);
        OrderTreeNode(root.left,depth+1);
        OrderTreeNode(root.right,depth+1);
    }


    //给你二叉树的根节点 root ，返回其节点值 自底向上的层序遍历 。
    List<List<Character>> levelOrderlist = new ArrayList<>();
    public List<List<Character>> levelOrderBottom(TreeNode root) {
        if(root == null){
            return levelOrderlist;
        }
        OrderTreeNode(root,0);
        return levelOrderlist;
    }

    private void OrderTreeNode1(TreeNode root,int depth){
        if(root == null){
            return;
        }
        if(levelOrderlist.size() == depth){
            levelOrderlist.add(0,new ArrayList<>());
        }
        levelOrderlist.get(levelOrderlist.size() - depth - 1).add(root.val);
        OrderTreeNode1(root.left,depth+1);
        OrderTreeNode1(root.right,depth+1);
    }
}
